package structure.Flyweight;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LYJ on 2016/10/19.
 */
public class FlyweightCompositeFactory {
    private Map<String,Flyweight > flyweightMap=new HashMap<>();
    public Flyweight factory(List<String > compositeState){
        ConcreteCompositeFlyweight compositeFlyweight=new ConcreteCompositeFlyweight();
        for(String s:compositeState){
            compositeFlyweight.add(s,this.factory(s));
        }
        return compositeFlyweight;
    }
    public Flyweight factory(String s){
        Flyweight fly=flyweightMap.get(s);
        if (null==fly){
            fly=new ConcreteFlyweight(s);
            flyweightMap.put(s,fly);
        }
        return fly;
    }
}
